<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Bulletin/classes/PhpFox_ComponentList.class.php, $Author:  $ */



/** Display list bulletins
 *
 * Parameters:
 *  1. oUser (mandatory) - user account object
 *
 * @package module.bulletin
 * @version $Id:
 *   PHPFox Team ()
 */
class PhpFox_Mod_Bulletin_ComponentList extends BaseComponent
{
	function process()
	{
        App::loadClass('Text');
        $oTxt = &new Text();

	    $oReq = &App::getRequest();
	    $oSrvBulletin = $this->_oModule->getService('Bulletin');
	    $oSrvAccount = &App::getModuleService('Account', 'Security');
	    $oSrvFriend  = &App::getModuleService('Account', 'Friends');
	    $oCurrUser = &$oSrvAccount->getCurrentUser();
	    $sCurrentUser = $oSrvAccount->getCurrentUserLogin();
	    $bIsAdmin = ($oSrvAccount->isAdmin() || $oSrvAccount->isStaff()) ? true : false;

	    if($oReq->getInt('del'))
	    {
	        if($sCurrentUser != $oSrvBulletin->getBulletinById($oReq->getInt('del'), 'user') && !$bIsAdmin)
	        {
	           App::gotoUrl('public.account.bulletin');
			}

	        $oUrl = App::getUrl();
	        $oUrl->setParam('del');
	        $oSrvBulletin->delItems($oReq->getInt('del'));
	        App::forward($oUrl->getUrl(), array(App::format('Mod_Bulletin.item.deleted')), 'message', App::getPageName(), 'Board');
	    }

	    // Edit item
	    if($oReq->getInt('edit') && ($bIsAdmin || ($sCurrentUser == $oSrvBulletin->getBulletinById($oReq->getInt('edit'), 'user'))))
	    {
	       App::gotoUrl('public.account.bulletin.compose', array('id' => $oReq->getInt('edit')));
		}

	    // Post replay item
	    if($oReq->getInt('rep'))
	    {
	        $aBulletinInfo = $oSrvBulletin->getBulletinById($oReq->getInt('rep'));

			if (!$bIsAdmin AND App::getUser('user') !=  $aBulletinInfo['user'] AND $oSrvFriend->checkIsFriend(App::getUser('user'), $aBulletinInfo['user']) == 0 )
			{
				App::gotoUrl('public.account.bulletin.view', array('id'=>$oReq->getInt('rep')));
        	}

        	$oTxt = &App::newObject('Text');

	        $_SESSION['compose']['user'] = $aBulletinInfo['user'];
	        $_SESSION['compose']['subject'] = ''. App::format('Mod_Bulletin.bulletin') .': '. $oTxt->preParseText($aBulletinInfo['title']);
            $sMessage = chr(10).chr(10).chr(10).chr(10).'--------- '. App::format('Mod_Bulletin.bulletin_posting') .' ----------------
[quote="'.$aBulletinInfo['user'].'"]
'. $oTxt->preParseText($aBulletinInfo['text']) .'
[/quote]';
            $_SESSION['compose']['message'] = $oTxt->preParseText($sMessage);
            App::gotoUrl('public.account.messages.compose');
	    }

	    // View item
	    if($oReq->getInt('view'))
	    {
	       App::gotoUrl('public.account.bulletin.view', array('id'=>$oReq->getInt('view')));
		}

	    $oTpl = &$this->_oModule->getTemplate();

       	$iPage = $oReq->get('page', 1);
       	$iPageSize = App::getParam('public.account.small.board');

		$aCond = array();
		if($oReq->get('my'))
		{
			$aCond[] = "`user` = '". App::getUser('user') ."' ";
		}
		elseif ( App::getParam('bFriendlyBoard') )
		{
			$sCnd = $oSrvFriend->getFriendsString( $oSrvAccount->getCurrentUser() );
        	$aCond['user'] = ' IN ("'.App::getUser('user').'"'.($sCnd? ','.$sCnd:'').')';
        	
		}
		$sOrder = 'time DESC';
		list($aBulletin, $iCnt) = $oSrvBulletin->listBulletins($aCond, $iPage, $iPageSize, $sOrder);
		
        foreach ($aBulletin as $i=>$aRes)
        {
            $aBulletin[$i]['owner']  = ($sCurrentUser == $aRes['user']) ? true : false;
        }

        $oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);

        $oTpl->assignSrc(array(
                                'aBulletin'    => !empty($aBulletin) ? $aBulletin : false,
                                'bIsAdmin'     => $bIsAdmin,
                                )
        );

        $oTpl->assign(array(
			                 'sBoxName'     => App::format('bb.board'),
			                 'aErrors'		=> $this->getErrors(),
                             'aPaging'      => $oPager->getInfo(App::getUrl()),
                             'sUserPicUrl' 	=> App::getParam('user.pic.url'),
			));

		$srcHtml = $oTpl->fetch('List.html');
		return $srcHtml;
	}
}
?>